<?php
class PublicsController extends AppController {

	var $name 		= "Publics";
	var $helpers 	= array('Javascript','Ajax'); 
	var $components = array('RequestHandler');
	var $uses = array("User","Grupo","Iglesia","Tipogrupo","Rol","Tiporol","Config","Oficio","Comuna","Sector");

	function index(){
		
	}
	function infovis(){
		
	}
	function loadFormGrid(){
		$response = array(
			"response"=>array(
				"error"=>false,
				"errorThread"=>"",
				"errorTitle"=>"",
				"showErrorThread"=>false,
				"type"=>"view", 
				"data"=>$this->render("afx/reportes/reportes"),
				"title"=>"Grupos"
			)
		);
		$this->outJson($response);
	}
	function loadInscritosView(){
		$tipos = $this->Tipogrupo->find('all',array(
						'fields' => array('Tipogrupo.id','Tipogrupo.nombre'),
						'recursive' => -1
					)
		);
		$iglesias = $this->Iglesia->find('all',array(
						'fields' => array('Iglesia.id','Iglesia.nombre'),
						'recursive' => -1
					)
		);
		$comunas = $this->Comuna->find('all',array(
						'fields' => array('Comuna.nombre','Comuna.id'),
						'order'=>"Comuna.nombre ASC",
						'recursive' => -1
					)
		);
		//cargamos inicialmente los sectores de Valdivia
		$sectores = $this->Sector->find('all',array(
						'conditions'=>array(
							"Sector.comuna_id"=>"14101"
						),
						'fields' => array('Sector.nombre','Sector.id'),
						'recursive' => -1
					)
		);
		$this->set("tipos",$tipos);
		$this->set("iglesias",$iglesias);
		$this->set("comunas",$comunas);
		$this->set("sectores",$sectores);
		$html = $this->render("afx/reportes/inscritos");
		$response = array(
			"response"=>array(
				"error"=>false,
				"errorThread"=>"",
				"errorTitle"=>"",
				"showErrorThread"=>false,
				"type"=>"view", 
				"data"=>$html,
				"title"=>"Nuevo Grupo"
			)
		);
		$this->outJson($response);
	}
	function loadReport(){
		$datos = array();
		$id = $_POST["tid"];
		$anio = $_POST["anio"];
		$imes = $_POST["imes"];
		$fmes = $_POST["fmes"];
		$iId = $_POST["iglesia_id"];
		
		$grupos = $this->Grupo->find('all',array(
						'conditions'=>array(
							"Grupo.tipogrupo_id"=>$id,
							"Grupo.iglesia_id"=>$iId
						),
						'recursive' => -1,
						'fields'=>array(
							"Grupo.id",
							"Grupo.nombre",
							"MONTH(Grupo.fecha_inicio) as mes",
							"YEAR(Grupo.fecha_inicio) as anio"
						)
					)
		);
		$tpg = $this->Tipogrupo->find('first',array(
						'conditions'=>array(
							"Tipogrupo.id"=>$id
						),
						'recursive' => -1
					)
		);
		
		
		$totales = array();
		$nf = array();
		$nm = array();
		$nLideres = array();
		$nDiscipulos = array();
		for($i=$imes+1;$i<=$fmes+1;$i++):
			$totalMes = 0;
			$nsf = 0;
			$nsm = 0;
			
			$nlideres = 0;
			$ndis = 0;
			$nmaestros = 0;
			$napostoles = 0;
			$nministros = 0;
			$nayudantes = 0;
			$nalumnos = 0;
			$noyentes = 0;
			foreach($grupos as $grupo):
				$gmes = $grupo["0"]["mes"];
				$ganio = $grupo["0"]["anio"];
				$np=0;
				$nrols=array();
				if($tpg["Tipogrupo"]["duracion"] == "D"):
					if($ganio == $anio && $gmes == $i ):
						$np = $this->Rol->find('count',array(
								'conditions'=>array(
									"Rol.grupo_id"=>$grupo["Grupo"]["id"],
								),
								'recursive' => -1
							)
						);
						if($np>0):
							$nrols = $this->Rol->find('all',array(
									'conditions'=>array(
										"Rol.grupo_id"=>$grupo["Grupo"]["id"]
									),
									'recursive' => -1,
									'fields'=>array(
										"Rol.user_id",
										"Rol.tiporol_id"
									)
								)
							);
						endif;	
					endif;	
				else:
					$np = $this->Rol->find('count',array(
							'conditions'=>array(
								"Rol.grupo_id"=>$grupo["Grupo"]["id"],
								"YEAR(Rol.created)"=>$anio,
								"MONTH(Rol.created)"=>$i,
							),
							'recursive' => -1
						)
					);
					if($np>0):
						$nrols = $this->Rol->find('all',array(
									'conditions'=>array(
										"Rol.grupo_id"=>$grupo["Grupo"]["id"],
										"YEAR(Rol.created)"=>$anio,
										"MONTH(Rol.created)"=>$i
									),
									'recursive' => -1,
									'fields'=>array(
										"Rol.user_id",
										"Rol.tiporol_id"
									)
								)
						);
					endif;	
				endif;
				foreach($nrols as $rol):
					$user = $this->User->find('first',array(
									'conditions'=>array(
										"User.id"=>$rol["Rol"]["user_id"]
									),
									'recursive' => -1,
									'fields'=>array(
										"User.sexo"
									)
								)
					);
					$tpr = $this->Tiporol->find('first',array(
									'conditions'=>array(
										"Tiporol.id"=>$rol["Rol"]["tiporol_id"]
									),
									'recursive' => -1,
									'fields'=>array(
										"Tiporol.nombrerol"
									)
								)
					);
					if($user["User"]["sexo"] == "F") $nsf++;
					else $nsm++; 
					if($tpr["Tiporol"]["nombrerol"] == "lider") $nlideres++;
					if($tpr["Tiporol"]["nombrerol"] == "discipulo") $ndis++;
				endforeach;	
				$totalMes = $totalMes+$np;
			endforeach;
			$datos[] = $totalMes;  //total de inscritos
			$nf[] = $nsf;			//total sexo femenino
			$nm[] = $nsm;			//total sexo masculino
			$nLideres[] = $nlideres;//total lideres
			$nDiscipulos[] = $ndis; //total discipulos
		endfor;	
		$response = array(
			"response"=>array(
				"error"=>false,
				"errorThread"=>"",
				"errorTitle"=>"",
				"showErrorThread"=>false,
				"type"=>"view", 
				"data"=>array(
					"totales"=>$datos,
					"totalf"=>$nf,
					"totalm"=>$nm,
					"nlideres"=>$nLideres,
					"ndisicpulos"=>$nDiscipulos
				),
				"title"=>"Nuevo Grupo"
			)
		);
		$this->outJson($response);
	}
	function loadCelulasLiderView(){
		$html = $this->render("afx/reportes/reporteporlider");
		$response = array(
			"response"=>array(
				"error"=>false,
				"errorThread"=>"",
				"errorTitle"=>"",
				"showErrorThread"=>false,
				"type"=>"view", 
				"data"=>$html,
				"title"=>"Nuevo Grupo"
			)
		);
		$this->outJson($response);
	}
	function loadReportByType(){
		$uroot = $this->User->find('first',array(
						'conditions'=>array(
							"User.root"=>"S"
						),
						'fields'=>array(
							"User.id"
						),
						'recursive' => -1
					)
		);
		$tpLider = $this->Tiporol->find('first',array(
									'conditions'=>array(
										"Tiporol.nombrerol"=>"lider"
									),
									'recursive' => -1,
									'fields'=>array(
										"Tiporol.id"
									)
								)
					);
		$tpDis = $this->Tiporol->find('first',array(
									'conditions'=>array(
										"Tiporol.nombrerol"=>"discipulo"
									),
									'recursive' => -1,
									'fields'=>array(
										"Tiporol.id"
									)
								)
					);			
		$gPrincipales = $this->Rol->find('all',array(
						'conditions'=>array(
							"Rol.user_id"=>$uroot["User"]["id"],
							"Rol.tiporol_id"=>$tpLider["Tiporol"]["id"]		
						),
						'fields'=>array(
							"Rol.grupo_id"
						),
						'recursive' => -1
					)
		);
		$data = array();
		foreach($gPrincipales as $g):
			$uPs = $this->Rol->find('all',array(
						'conditions'=>array(
							"Rol.grupo_id"=>$g["Rol"]["grupo_id"],
							"Rol.tiporol_id"=>$tpDis["Tiporol"]["id"]		
						),
						'fields'=>array(
							"Rol.user_id"
						),
						'recursive' => -1
					)
			);
			foreach($uPs as $u):
				$userx = $this->User->find('first',array(
								'conditions'=>array(
									"User.id"=>$u["Rol"]["user_id"]
								),
								'fields'=>array(
									"User.nombre1",
									"User.nombre2",
									"User.apellido1",
									"User.apellido2",
								),
								'recursive' => -1
							)
				);
				
				$this->User->dis = array();
				$this->User->ncelulas = 0;
				$this->User->ncount = 0;
				$this->tipoDescendencia = 12;
				
				$this->User->getMiracleData($u["Rol"]["user_id"]);
				
				//print_r($this->User->dis);
				
				$data[] = array(
					"user"=>$userx["User"],
					"dis"=>$this->User->dis,
					"ncelulas"=>$this->User->ncelulas,
					"nhijos"=>$this->User->ncount
				);
			endforeach;
		endforeach;
		
		/*
//		$data = $this->User->getMiracleData(653); //robinson
//		$data = $this->User->getMiracleData(617); //pastora
		//$data = $this->User->getMiracleData(483); //alejandra
		$data = $this->User->getMiracleData(644); //alejandra
		echo "hijos espirituales de lider: ".$this->User->ncount.", desendencia segun tipo: ";
		print_r($this->User->dis);
		echo ", posee ".$this->User->ncelulas." celulas";
		*/
		$response = array(
			"response"=>array(
				"error"=>false,
				"type"=>"list", 
				"data"=>$data
				)
			);
		$this->outJson($response);
	}
	function loadCelulasLiderReport(){
		$tpg = $this->Tipogrupo->find('first',array(
										'conditions'=>array(
											"Tipogrupo.nombretipo"=>"celula"
										),
										'recursive' => -1
									)
						);
		$users = $this->User->find('all',array(
						'fields'=>array(
							"User.nombre1",
							"User.nombre2",
							"User.apellido1",
							"User.apellido2",
							"User.id"
						),
						'recursive' => -1
					)
		);
		$liderRol = $this->Tiporol->find('first',array(
										'conditions'=>array(
											"Tiporol.nombrerol"=>"lider"
										),
										'recursive' => -1
									)
						);
		$disRol = $this->Tiporol->find('first',array(
										'conditions'=>array(
											"Tiporol.nombrerol"=>"discipulo"
										),
										'recursive' => -1
									)
						);	
						/*			
		foreach($users as $u):
			$np = $this->Rol->find('count',array(
							'conditions'=>array(
								"Rol.user_id"=>$u["User"]["id"],
								"Rol.tiporol_id"=>$liderRol
							),
							'recursive' => -1
						)
				);
		endforeach;
		*/
	}
}
?>